<?php

namespace App\Services\Utils;

use App\Models\Charge;
use Illuminate\Support\Carbon;
use App\Models\ApiKey;
use App\Models\ApiKeyRequestLog;
use Thread;

class Consumption extends Thread
{
    public $api_key;
//    public $amount;
    public $response;

    public function __construct($api_key, $amount)
    {
        $this->api_key = $api_key;
//        $this->amount = $amount;
    }

    public function run()
    {
        $this->response = '233';
        sleep(10);
//        $apikey = ApiKey::where('key', $this->api_key)->first();
//        dd($apikey);
//        $log_id = ApiKeyRequestLog::where('app_id', $apikey->id)->latest()->value('id');
//        $change_before = Charge::where('customer_id', $apikey->customer_id)->latest()->value('change_after');
//        $change_after = $change_before - $this->amount;
//        $change_type = 'apikey_consume';
//        $params = array(
//            'log_id' => $log_id
//        );
//        $remark = 'apikey请求扣费' . ($this->amount/100.0) . '元';
//
//        $charge = new Charge;
//        $charge->customer_id = $apikey->customer_id;
//        $charge->change_before = $change_before;
//        $charge->change_after = $change_after;
//        $charge->change_type = $change_type;
//        $charge->change_params = json_encode($params);
//        $charge->remark = $remark;
//        $charge->created_at = Carbon::now();
//        $charge->save();
    }
}
